﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.mIE.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mIE.UI
{
    public partial class Frm_IE_Batch_UnLockTask : frmBaseChild
    {
        DataTable dtProc_Type_Ctrl = new DataTable();
        bool flag = true;
        public Frm_IE_Batch_UnLockTask()
        {
            InitializeComponent();
        }
        public Frm_IE_Batch_UnLockTask(DataTable dt)
        {
            InitializeComponent();
            dtProc_Type_Ctrl = dt;
        }
        private void Frm_IE_Batch_UnLockTask_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            txtPart_No.Enabled = false;
            InitProductItem(dtProc_Type_Ctrl);
            this.radioButton1.Checked = true;
            //鼠标悬停显示
            toolTip1.IsBalloon = true;
            toolTip1.SetToolTip(this.radioButton1, "表面解锁：仅清除计算及上载标识");
            toolTip1.SetToolTip(this.radioButton2, "深度解锁：删除工艺、周期、尺寸及特征相关数据并清除对应标识");　
        }

        //产品类型初始化
        public void InitProductItem(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                this.txtProductItem.Properties.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.txtProductItem.Properties.Items.Add(dt.Rows[i]["PROD_CATEGORY"].ToString());
                }
                txtProductItem.SelectIndex = 0;
                txtProductItem.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            }
            else
            {
                dt = BaseFactory<IE_Part_TaskList>.Instance.GetProductItem();
                this.txtProductItem.Properties.Items.Clear();
                if (dt.Rows.Count > 0)
                {
                    string[] list = dt.Rows[0]["FIELD_VALUE_OPTION"].ToString().Split(',');
                    foreach (var item in list)
                    {
                        this.txtProductItem.Properties.Items.Add(item);
                    }
                }
            }
        }
        //保存
        public override void DoSave(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("IE_Part_TaskList", "LamexMESManager")) return;
            #endregion
            StringBuilder sbFilter1 = new StringBuilder();
            List<string> Temp1 = new List<string>();
            //获取选中行号
            int result = 0;
            int[] rownumber = this.gv_Main.GetSelectedRows();
            if (rownumber.Length <= 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请勾选需要解锁的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (ConfirmOK() == DialogResult.Cancel)
                return;
            try
            {
                //根据行号获取相应行的数据
                foreach (var item in rownumber)
                {
                    DataRow row = this.gv_Main.GetDataRow(item);
                    string DWGNO = row[1].ToString();
                    string PARTNO = row[2].ToString();
                    string sql_Update = "";
                    string sql_delete = "";

                    #region 按图纸号解锁
                    if (flag)
                    {                     
                        if (this.radioButton1.Checked)//表面解锁
                        {
                            sql_Update = @"update [LamexMESManager].[dbo].[IE_Part_TaskList] set CAL_FLAG='',UPLOAD_FLAG='' 
                where DWG_NO='" + DWGNO + "' and ISNULL(UPLOAD_FLAG,'')='1' ";
                        }
                        else//深度解锁
                        {
                            //根据图纸号获取部件号
                            DataTable dt = BaseBuildSql.GetDataTableBySql("select PART_NO from [LamexMESManager].[dbo].[IE_Part_TaskList] where DWG_NO='" + DWGNO + "'");
                            if (dt.Rows.Count <= 0)
                                continue;
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                Temp1.Add(dt.Rows[i]["PART_NO"].ToString());
                            }
                            sbFilter1.AppendFormat(@" AND PART_NO IN ('{0}')", string.Join("','", Temp1.ToArray()));
                            sql_delete = "delete from [LamexMESManager].[dbo].[IE_Part_CycleTime] where 1=1 " + sbFilter1 + " \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_FeatureList] where 1=1 " + sbFilter1 + " \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_Routing] where 1=1 " + sbFilter1 + " \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_SizeParmList] where 1=1 " + sbFilter1 + " \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_SizeParmList_Fabric]  where 1=1 " + sbFilter1 + " \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_SizeParmList_Foam] where 1=1 " + sbFilter1 + " \r\n";
                            sql_Update = @"update [LamexMESManager].[dbo].[IE_Part_TaskList] set CAL_FLAG='',UPLOAD_FLAG='',ROUTING_FLAG='',PARM_FLAG='',FEAT_FLAG=''  
                where DWG_NO='" + DWGNO + "' and ISNULL(UPLOAD_FLAG,'')='1' ";
                            int n = BaseBuildSql.ExecuteNonQuery(sql_delete);
                        }
                        int m = BaseBuildSql.ExecuteNonQuery(sql_Update);
                    }
                    #endregion
                    #region 按部件号解锁
                    else
                    {
                        if (this.radioButton1.Checked)//表面解锁
                        {
                            sql_Update = @"update [LamexMESManager].[dbo].[IE_Part_TaskList] set CAL_FLAG='',UPLOAD_FLAG='' 
                where DWG_NO='" + DWGNO + "' and PART_NO='" + PARTNO + "' and ISNULL(UPLOAD_FLAG,'')='1' ";
                        }
                        else
                        {
                            sql_delete = "delete from [LamexMESManager].[dbo].[IE_Part_CycleTime] where 1=1 and PART_NO='" + PARTNO + "' \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_FeatureList] where 1=1 and PART_NO='" + PARTNO + "' \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_Routing] where 1=1 and PART_NO='" + PARTNO + "' \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_SizeParmList] where 1=1 and PART_NO='" + PARTNO + "' \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_SizeParmList_Fabric]  where 1=1 and PART_NO='" + PARTNO + "' \r\n";
                            sql_delete += "delete from [LamexMESManager].[dbo].[IE_Part_SizeParmList_Foam] where 1=1 and PART_NO='" + PARTNO + "' \r\n";
                            sql_Update = @"update [LamexMESManager].[dbo].[IE_Part_TaskList] set CAL_FLAG='',UPLOAD_FLAG='',ROUTING_FLAG='',PARM_FLAG='',FEAT_FLAG=''  
                where DWG_NO='" + DWGNO + "' and PART_NO='" + PARTNO + "' and ISNULL(UPLOAD_FLAG,'')='1' ";
                            int n = BaseBuildSql.ExecuteNonQuery(sql_delete);
                        }
                        int m = BaseBuildSql.ExecuteNonQuery(sql_Update);
                    }
                    #endregion
                }

                DevExpress.XtraEditors.XtraMessageBox.Show("数据保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                grid_Main.DataSource = new DataTable();
                memoDWG_NO.Text = "";
                txtPart_No.Text = "";
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        public DialogResult ConfirmOK()
        {
            return DevExpress.XtraEditors.XtraMessageBox.Show("请再次确认更改信息无误后，点击确定！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
        }
        //刷新
        public override void DoView(IButtonInfo sender)
        {
            grid_Main.DataSource = new DataTable();
            string sql_DwgNo = "", sql_PartNo = "";
            StringBuilder sbDRAWING = new StringBuilder();
            StringBuilder sbPARTNO = new StringBuilder();
            string[] str1 = memoDWG_NO.Text.ToUpper().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string[] str2 = txtPart_No.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            if (txtProductItem.Text.Length <= 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择产品类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (str1.Length <= 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入图纸编号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (str1.Contains("NIL") && str2.Length <= 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入物料编号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (str1.Length > 0)
            {
                sbDRAWING.AppendFormat(" AND T.DWG_NO IN('{0}') ", string.Join("','", str1.ToArray()));
                sql_DwgNo = sbDRAWING.ToString();
            }
            else
            {
                sql_DwgNo = "and T.DWG_NO like '%%'";
            }
            if (str2.Length > 0)
            {
                sbPARTNO.AppendFormat(" AND T.PART_NO IN('{0}') ", string.Join("','", str2.ToArray()));
                sql_PartNo = sbPARTNO.ToString();
            }
            else
            {
                sql_PartNo = "and T.PART_NO like '%%'";
            }
            string sql_Pro = txtProductItem.Text.Length > 0 ? " and T.PROD_CATEGORY='" + txtProductItem.Text + "'" : " and T.PROD_CATEGORY like '%%'";
            string sqlWhere = sql_Pro + sql_DwgNo + sql_PartNo;
            string sql = "";
            if (flag)
            {
                sql = @"SELECT distinct [PROD_CATEGORY]
      ,[DWG_NO]
      ,'**********' as [PART_NO]
  FROM [LamexMESManager].[dbo].[IE_Part_TaskList] T
  LEFT jOIN [dbo].[IE_Part_MasterData] M ON T.PART_NO=M.PART_NO where 1=1  and ISNULL(UPLOAD_FLAG,'')='1' ";
            }
            else
            {
                sql = @"SELECT [PROD_CATEGORY]
      ,[DWG_NO]
      ,T.[PART_NO]
      ,M.[CN_DESC]
      ,M.MODEL_NO  
  FROM [LamexMESManager].[dbo].[IE_Part_TaskList] T
  LEFT jOIN [dbo].[IE_Part_MasterData] M ON T.PART_NO=M.PART_NO where 1=1  and ISNULL(UPLOAD_FLAG,'')='1' ";
            }
            sql = sql + sqlWhere + "  ";

            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                grid_Main.DataSource = dt;
                gv_Main.BestFitColumns();
                this.gv_Main.OptionsView.ColumnAutoWidth = false;
            }
            else
            {              
                DevExpress.XtraEditors.XtraMessageBox.Show("查不到任何数据，请确认输入条件是否正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        private void gv_Main_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.C)
            {
                Clipboard.SetDataObject(gv_Main.GetFocusedRowCellValue(gv_Main.FocusedColumn));
                e.Handled = true;
            }
        }

        private void memoDWG_NO_TextChanged(object sender, EventArgs e)
        {
            string[] str1 = memoDWG_NO.Text.ToUpper().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            if (str1.Contains("NIL"))
            {
                txtPart_No.Enabled = true;
                flag = false;
            }
            else
            {
                flag = true;
                txtPart_No.Text = "";
                txtPart_No.Enabled = false;
            }
        }
    }
}
